const axios = require("axios");
const fs = require("fs");
const cheerio = require("cheerio");
const htmlToMd = require("html-to-md");
const config = require("./config.json");

const queryList = async (params) => {
  return new Promise((resolve) => {
    axios
      .post(
        "https://api.juejin.cn/content_api/v1/article/query_list?aid=2608&uuid=7248590178503558696&spider=0",
        params
      )
      .then((response) => {
        resolve(response.data);
      })
      .catch((error) => {
        console.error(error);
      });
  });
};

const getDetail = async (id) => {
  const response = await axios.get(`https://juejin.cn/post/${id}`);
  const html = response.data;
  const $ = cheerio.load(html);
  const article = $(
    "#juejin > div.view-container > main > div > div.main-area.article-area > article"
  ).html();
  let title = $(
    "#juejin > div.view-container > main > div > div.main-area.article-area > article > h1"
  )
    .text()
    .replace(/\n|\t| /g, "");
  title = title.replace(/\|/g, "-").trim();
  const content = $("#article-root").html();
  fs.writeFileSync(`html/${title}.html`, article);
  const markdownContent = htmlToMd(content);
  fs.writeFileSync(`md/${title}.md`, markdownContent);
  console.log(`已导出文章《${title}》`);
};

async function sleep(times = 1000) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, times);
  });
}
const getListJson = async () => {
  let list = [];
  const params = {
    user_id: config.user_id,
    sort_type: 2,
    cursor: "0",
  };
  let has_more = true;
  while (has_more) {
    const res = await queryList(params);
    params.cursor = parseInt(params.cursor) + 10 + "";
    has_more = res.has_more;
    res.data && list.push(...res.data);
    console.log(`正在导出文章列表数据${params.cursor}/${res.count}`);
    if(list.length === res.count) break;
    await sleep(2000);
  }
  fs.writeFileSync("list.json", JSON.stringify(list, null, 2));
  console.log("文章列表数据已全部导出");
};
const getDetailBylist = async () => {
  let index = config.lastIndex || 0;
  try {
    const list = require("./list.json");
    const len = list.length;
    for (index; index < len; index++) {
      const i = index;
      console.log(`正在导出《${list[i].article_info.title}》${i + 1}/${len}……`);
      await getDetail(list[i].article_id);
      await sleep(5000);
    }
    console.log("已全部导出");
  } catch (err) {
    console.error(err);
    config.lastIndex = index;
    fs.writeFileSync("config.json", JSON.stringify(config, null, 2));
    console.log("导出出错终止，30秒后自动重试");
    await sleep(30000);
    getDetailBylist();
  }
};
const createDir = async()=>{
  if(!fs.existsSync("html")){
    fs.mkdirSync('html');
  }
  if(!fs.existsSync("md")){
    fs.mkdirSync('md');
  }
};
const run = async () => {
  await getListJson();
  await createDir();
  await getDetailBylist();
};

run();
